/*
Content: Linkage between PASS and events
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Journal: Journal of Labor Economics
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/


********************************************************************************
**# LOAD MAIN DATASET
********************************************************************************

use "\\IAB\dfs\017\Ablagen\D01700-IAB-Projekte\D01700-Projekte-FDZ\Datensaetze\_Endprodukte\PASS\PASS_0621_v2\PENDDAT.dta", clear

* add country of origin

merge m:1 pnr welle using "$datapass/ogeblandna.dta", gen(merge_corigin)

		
********************************************************************************
**# CREATE VARIABLES
********************************************************************************
	
* TIMING OF INTERVIEW 

clonevar startdate = pintdat

g year=pintjahr
g month=pintmon
g day = pinttag
g startmy=ym(year, month)
format startmy %tm

* PERSONAL ID 

clonevar pid = pnr 

* GENDER

gen sex = zpsex if zpsex != . & zpsex >0 

* EDUCATION 

recode beruf2 (-10/2 = 1) (3/8 =2) (9/20 = 3), gen(edu_3cat)

* MARITAL STATUS 

gen maritstat = famstand 
replace maritstat = 99 if maritstat == . | maritstat <0 

* CHILD

gen child = 0 if kindzges == 0 
replace child = 1 if kindzges == 1
replace child = 2 if kindzges > 1 & kindzges != .
replace child = 99 if child ==. 

* AGE 

gen age = palter if palter > 0 & palter != .

* YEARS SINCE MIGRATION

gen immiyear = PMI0300b if PMI0300b > 0 & PMI0300b !=.

gen ysm = year - immiyear
tab ysm



********************************************************************************
*** STEP 4: MERGE PASS WITH MONTHLY EVENT DATA
********************************************************************************



drop if year==. // ATENTION DROP WHEN NO SURVEY DATA

drop  year month

br ogeblandna startmy remain_ger_per startmy 

tab startmy


fmerge m:1 ogeblandna startmy  using "$globalterror/final/terror_monthlydata_pass_v1.dta", gen(eventmerge_m)
drop if eventmerge_m==2



cap drop year
cap drop month
cap drop day 
cap drop week
cap drop startmy

g year=pintjahr
g month=pintmon
g day = pinttag
g startmy=ym(year, month)
format startmy %tm


******************************************
**** INTERVIEWED PRE-POST EVENT ******
******************************************
			
*Pre-post event
foreach x in 30 60 90 {
foreach event in terror  {	
foreach var in	 m5y m4y m3y {		
cap drop post`event'_`var'_p`x' 
g post`event'_`var'_p`x' =0 if distance`event'_`var'_p`x' <=0
replace post`event'_`var'_p`x' =1 if distance`event'_`var'_p`x'>=1 & distance`event'_`var'_p`x'!=.
}
}
}


foreach event in terror     {	
foreach var in	 m5y m4y m3y  {

label def post`event'_`var'_p90 0 "Pre-Terror" 1 "Post-Terror", replace
label val post`event'_`var'_p90 post`event'_`var'_p90
label var post`event'_`var'_p90 "Terror"

}
}

*****************************************************************************
** Keep only events were there are people in the control and treatment group
*****************************************************************************
* I do this with the share of interviews before and after - if share =1 means all interviews took place in the  pre-period or all in post period

foreach var in	m5y m4y m3y {
foreach x in	90 60 30 {
* Sum by country of origin, event date and pre-porst, sum number of interviews
cap drop prepostinterterror_`var'_p`x' 
cap drop totinterterror_`var'_p`x'  
cap drop ratiointerterror_`var'_p`x'
cap drop mratiointerterror_`var'_p`x'
qui bys ogeblandna  postterror_`var'_p`x' relevantterror_`var'_p`x' : gen prepostinterterror_`var'_p`x'  = _N   if distanceterror_`var'_p`x'!=. // for a given country and event, this sums total interviews in pre-period (e.g. among control group) and post peridod (e.g. among treatment group)
qui bys ogeblandna relevantterror_`var'_p`x' : gen totinterterror_`var'_p`x'  = _N  if distanceterror_`var'_p`x'!=. // // for a given country and event, this sums total interviews 
qui g ratiointerterror_`var'_p`x'= prepostinterterror_`var'_p`x'/totinterterror_`var'_p`x' if distanceterror_`var'_p`x'!=.
qui bys ogeblandna relevantterror_`var'_p`x' : egen mratiointerterror_`var'_p`x'=max(ratiointerterror_`var'_p`x') if ratiointerterror_`var'_p`x'!=.
cap drop prepostinterterror_`var'_p`x' totinterterror_`var'_p`x'
}
}



save "$datapass/finaldata_terrorpass_final.dta", replace


***************************
*** SAMPLE
***************************
**** ATTENTON: match sample time period to IEB
drop if year>2019
drop if year<2000
drop if ogeblandna==1 // drop Germans



*** Keep sample	with and without controls constant
foreach var in	m5y m4y m3y {
foreach x in	90 60 30 {
egen clusterterror_`var'_p`x'=group(relevantterror_`var'_p`x')
}
}



eststo clear
local i=1				
foreach var in	 m5y m4y m3y  {

global select_reg_M "!inlist(ogeblandna,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0 &  mratiointerterror_m3y_p90!=1"
global controls "i.ogeblandna#i.year i.month#i.year" 	

qui: reghdfe remain_ger_per postterror_`var'_p90  if $select_reg_M, vce(cluster clusterterror_`var'_p90) absorb($controls)
cap drop  sample_terror_`var'
qui:  g sample_terror_`var'=e(sample)

}


save "${data}/finaldata_terrorpass_light_final.dta", replace


